package com.beone.smart.smartBeauty.controller.webApi;

import com.beone.smart.smartBeauty.exception.GlobalException;
import com.beone.smart.smartBeauty.model.mongodb.ShowFile;
import com.beone.smart.smartBeauty.model.mongodb.UploadFile;
import com.beone.smart.smartBeauty.model.result.ResultEntity;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by bruce on 20/2/15 -- 19:24
 */
@Api(description = "图片上传下载")
@Slf4j
@RestController
@RequestMapping(value = "/file")
public class FileController {
    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation(value = "获取单张图片", notes = "查看图片")
    @ApiImplicitParam(name = "id", value = "图片id", required = true)
    @GetMapping(value = "/fetch/{id}",
            produces = {
                    MediaType.IMAGE_JPEG_VALUE,
                    MediaType.IMAGE_PNG_VALUE
            })
    public byte[] image(@PathVariable String id) {
        byte[] data = null;
        UploadFile file = mongoTemplate.findById(id, UploadFile.class);
        if (file != null) {
            data = file.getContent().getData();
        }
        return data;
    }

    @ApiOperation(value = "获取图片列表", notes = "全部图片")
    @GetMapping(value = "/list")
    public ResultEntity list() {
        List<UploadFile> list = mongoTemplate.findAll(UploadFile.class);
        List<ShowFile> resultList = new ArrayList<>();
        for (UploadFile uploadFile : list) {
            ShowFile showFile = new ShowFile();
            showFile.setId(uploadFile.getId());
            showFile.setName(uploadFile.getName());
            showFile.setSize(uploadFile.getSize());
            showFile.setCreatedTime(uploadFile.getCreatedTime());
            resultList.add(showFile);
        }
        return ResultTool.ok(resultList);
    }

    @ApiOperation(value = "上传图片", notes = "单张上传，不支持批量")
    @PostMapping("/uploadImage")
    public ResultEntity uploadImage(@RequestParam(value = "image") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new GlobalException(ResultTool.HttpResponseCode.FAIL, "请选择一张图片");
        }
        String fileName = file.getOriginalFilename();
        UploadFile uploadFile = new UploadFile();
        uploadFile.setName(fileName);
        uploadFile.setCreatedTime(new Date());
        uploadFile.setContent(new Binary(file.getBytes()));
        uploadFile.setSize(file.getSize());
        UploadFile savedFile = mongoTemplate.save(uploadFile);
//        String url = "http://localhost:8080/file/image/" + savedFile.getId();
        return ResultTool.ok(uploadFile.getId(), "上传成功");

    }

}
